package com.dzm.xgb.core.controller.api;

import com.dzm.common.exception.Assert;
import com.dzm.common.result.R;
import com.dzm.common.result.ResponseEnum;
import com.dzm.common.util.JwtUtils;
import com.dzm.xgb.core.hfb.RequestHelper;
import com.dzm.xgb.core.pojo.vo.UserBindVo;
import com.dzm.xgb.core.service.UserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 作者：Dzm
 * 时间：2024-09-12-19:05
 * 功能：
 */

@Api(tags = "会员账号绑定")
@RestController
@RequestMapping("/api/core/userBind")
public class UserBindController {

    @Autowired
    private UserBindService userBindService;

    @ApiOperation("账户绑定提交数据")
    @PostMapping("/auth/bind")
    public R bind(@RequestBody UserBindVo vo, HttpServletRequest request) {
        //获取用户id
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        //获取用户绑定的表单返回给前端
        String formStr = userBindService.userBind(vo, userId);
        return R.ok().data("formStr", formStr);
    }

    /**
     * 异步回调，汇付宝发来的请求
     * @param
     * @return
     */
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        if(!RequestHelper.isSignEquals(map)) {
            return "fail";
        }

        userBindService.bindNotify(map);
        return "success";
    }
}
